//
//  FRPPhotoViewController.m
//  FunctionalReactivePixels
//
//  Created by sun on 15/7/5.
//  Copyright (c) 2015年 sunfei. All rights reserved.
//

#import "FRPPhotoViewController.h"

#import "FRPPhotoImporter.h"
#import <SVProgressHUD/SVProgressHUD.h>

#import "FRPPhotoModel.h"
#import "FRPPhotoViewModel.h"

@interface FRPPhotoViewController ()

//@property (nonatomic, readwrite) FRPPhotoModel *photoModel;
@property (nonatomic, readwrite) NSInteger photoIndex;
@property (nonatomic, weak) UIImageView *imageView;
@property (nonatomic, strong) FRPPhotoViewModel *viewModel;

@end

@implementation FRPPhotoViewController

//- (instancetype)initWithPhotoModel:(FRPPhotoModel *)model index:(NSInteger)index {
//    self = [super init];
//    if (self) {
//        self.photoIndex = index;
//        self.photoModel = model;
//    }
//    return self;
//}

- (instancetype)initWithViewModel:(FRPPhotoViewModel *)viewModel index:(NSInteger)photoIndex {
    self = [super init];
    if (self) {
        self.viewModel = viewModel;
        self.photoIndex = photoIndex;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor blackColor];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
//    RAC(imageView, image) = [RACObserve(self.photoModel, fullsizedData) map:^id(id value) {
//        return [UIImage imageWithData:value];
//    }];
    RAC(imageView, image) = RACObserve(self.viewModel, photoImage);
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    [self.view addSubview:imageView];
    self.imageView = imageView;
    
    [RACObserve(self.viewModel, loading) subscribeNext:^(NSNumber *loading) {
        if (loading.boolValue) {
            [SVProgressHUD show];
        } else {
            [SVProgressHUD dismiss];
        }
    }];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
//    [SVProgressHUD show];
//    [[FRPPhotoImporter fetchPhotoDetails:self.photoModel] subscribeError:^(NSError *error) {
//        [SVProgressHUD showErrorWithStatus:@"Error"];
//    } completed:^{
//        [SVProgressHUD dismiss];
//    }];
    self.viewModel.active = YES;
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    self.viewModel.active = NO;
}

@end
